﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Net;
using System.Net.Sockets;
using Emgu.CV;
using Emgu.CV.Structure;
using FaceRecognition.Models;
using FaceRecognition.View;
using FaceRecognition.Utilities;
namespace FaceRecognition
{
    public partial class Form_AddUser : Form
    {
        private Form_Training train_form;
        private Controller_Class control;
        private Form_UpdateUser updateform;
        private Form_DeleteUser deleteform;
        private Int32 id;
        public Form_AddUser()
        {
            InitializeComponent();

            control = new Controller_Class();
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
            this.Dispose();
            if (e.CloseReason == CloseReason.WindowsShutDown) return;

            // Confirm user wants to close
            //switch (MessageBox.Show(this, "Are you sure you want to close?", "Closing", MessageBoxButtons.YesNo))
            //{
            //    case DialogResult.No:
            //        e.Cancel = true;
            //        break;
            //    default:
            //        break;
            //}

        }
        private void button_insert_Click(object sender, EventArgs e)
        {

            // textboxes Validation
            //if (img_temp == null)
            //{
            //    MessageBox.Show("We couldn't take your picture  ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    return;
            //}
   
            if (textBox_email.Text == "" || textBox_fname.Text == "" || textBox_lname.Text == "" || textBox_phone.Text == null)
            {
                MessageBox.Show(" Et2ked mn el textboxes ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (!General_Usage.isEmail(textBox_email.Text))//Email validation 7aga krooky kda l7d manshof ezay hancheck eza kan exist asln wala la2 in the real world
            {
                MessageBox.Show(" the Email is not valid ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            try
            {
                if (control.User.email_exist(textBox_email.Text))
                {
                    MessageBox.Show(" the Email is used before please enter another email ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            catch
            {
                MessageBox.Show("Couldn't connect to Database", "DataBase Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (!check_Phone(textBox_phone.Text))
            {

                MessageBox.Show(" The phone must be 11 digit and must be numbers ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //1- call add new user function ( validate on email if it's olready exist or not)
            //the function will return true or false
            //without validation to the date bocla

            try
            {
                control.User.insert_user_info(textBox_fname.Text, textBox_lname.Text, textBox_phone.Text, textBox_email.Text);
           
            }
            catch
            {
                MessageBox.Show("We couldn't connect to the database ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            id = control.User.ID_retrival_by_email(textBox_email.Text);
            train_form = new Form_Training(id);
            train_form.Show();

            this.Close();
            //if true ( show dialog box you have been added successfuly)
            // else the email is olready exist


            //if true
            //  MessageBox.Show("You have been added Successfully ", "Done", MessageBoxButtons.OK,MessageBoxIcon.Information); 
            //else
            //MessageBox.Show("The Email is olready exist , Check it again  ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        //m3rfsh tfaseel el func dy ana l2etha 3la el net
        
        private void button_clear_Click(object sender, EventArgs e)
        {
            // Clear textboxes
            textBox_fname.Clear();
            textBox_lname.Clear();
            textBox_email.Clear();
            textBox_phone.Clear();
            //////////////////////
        }

        private bool check_Phone(string phone)
        {
            if (phone.Length != 11)
                return false;
            for (int i = 0; i < phone.Length; i++)
            {
                if (phone[i] - '0' < 0 || phone[i] - '0' > 9)
                {
                    return false;
                }
            }
            return true;
        }
       

        private void button_Exit_Click(object sender, EventArgs e)
        {

          //  face_detect_object.Close_Cam();
            this.Dispose();
        }


        private void button2_Click(object sender, EventArgs e)
        {
            updateform = new Form_UpdateUser();
            updateform.Show();
            //control.update_form();
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            deleteform = new Form_DeleteUser();
            deleteform.Show();
          
            
        }

        private void retrive_id_button_Click(object sender, EventArgs e)
        {
           
            id_retrieval.Text = "ID = " + Convert.ToString(id);
        }


      


    }
}
